home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 August: Tool Chest / Dev.CD Aug 00 TC Disk 2.toast / pc / sample code / interapplication comm / aecdev.aedaemon / aedaemonmain.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-06-23  |  5.0 KB  |  125 lines

  1. /*
  2.     File:        AEDaemonMain.c
  3.  
  4.     Contains:    This is a faceless background process who's whole mission in
  5.     life is to send AppleEvents for code that can't (like the accompaning
  6.     AECDEV).  It will send any AppleEvent it gets through it's PPC port.
  7.     
  8.     The main things to note are....
  9.     1)  Look at the SIZE resource for the flag settings you'll
  10.          need to let the Finder™ know that you only want to work
  11.          in the background.
  12.     2)  Notice that you really do have your own heap and your own
  13.         event loop.  You can do anything a foreground application
  14.         can do, send AppleEvents, PPC stuff, anything else you'd
  15.         like EXCEPT a graphic interface.
  16.     3)  NOTE that no managers are started up.  You cannot start up
  17.         Window, Menu, Dialogs, or anything else that 
  18.         deals with the graphic interface.  Just leave them out.
  19.         You CAN start up QuickDraw if you want to do some graphic 
  20.         processing, but DO NOT draw to the screen, only use things 
  21.         like offscreen grafports or GWorlds.  Or you may 
  22.         want to start it to use Random().....
  23.     
  24.     Of course, a backgrounder can be launched from the Finder™ 
  25.     with a double-click.  However, if you'd like the backgrounder
  26.     to perform some useful service for your main application, driver,
  27.     DA, or whatever, you will want it running all the time.
  28.     The BEST way to insure this is to install the backgrounder in the
  29.     StartUp Items folder in the system folder.  This will insure that
  30.     it is always launched, and it will also reduce memory fragmentation
  31.     since it will be installed at startup time.  You can search for it
  32.     when you need it with IPCListPorts (see the PPC toolbox documentation
  33.     or the AECDEV code).
  34.     Optionally, you can use the LaunchApplication trap to launch it 
  35.     when you need it, and kill it when you're done.  But this could 
  36.     cause some multiFinder heap fragmentation.
  37.     
  38.     And remember, it's a backgrounder, you can't see it.  To kill it
  39.     use TaskIt or ProcDoggie.
  40.     
  41.     ---------------------------------------------------------------
  42.     Use this sample as a starting point, and adapt its' routines to 
  43.     meet the specific needs of your project.  
  44.     This sample will grow as more edition types are implemented,
  45.     periodically check AppleLink for a later, expanded sample.
  46.     This application is an example of the form of a Macintosh 
  47.     application; it is NOT a template. It is NOT intended to be 
  48.     used as a foundation for the next world-class, best-selling, 
  49.     600K application. A stick figure drawing of the human body may 
  50.     be a good example of the form for a painting, but that does not 
  51.     mean it should be used as the basis for the next Mona Lisa.
  52.  
  53.     Written by: C.K. Haun    
  54.  
  55.     Copyright:    Copyright © 1991-1999 by Apple Computer, Inc., All Rights Reserved.
  56.  
  57.                 You may incorporate this Apple sample source code into your program(s) without
  58.                 restriction. This Apple sample source code has been provided "AS IS" and the
  59.                 responsibility for its operation is yours. You are not permitted to redistribute
  60.                 this Apple sample source code as "Apple sample source code" after having made
  61.                 changes. If you're going to re-distribute the source, we require that you make
  62.                 it clear in the source that the code was descended from Apple sample source
  63.                 code, but that you've made changes.
  64.  
  65.     Change History (most recent first):
  66.                 7/20/1999    Karl Groethe    Updated for Metrowerks Codewarror Pro 2.1
  67.                 
  68.  
  69. */
  70.  
  71.  
  72. #include "AEDaemon.h"
  73.  
  74. extern MyPPCRecPtrDeamon ourPPCPtr;
  75. extern Ptr readBuffer;
  76. extern Handle dataHandle;
  77. Boolean gQuit = false;
  78. EventRecord ERecord;
  79. Boolean gHasAppleEvents;
  80. Boolean gReadAgain;
  81. Boolean gReadPending;
  82. unsigned long gMySleep = 200;  /* Long time.  Change this if  */
  83. /* you'd like to do null processing. Just keep in mind that the */
  84. /* user does NOT know that you exisist, and if you are eating */
  85. /* up a bunch of time the user will not know why his or her */
  86. /* machine is slowing down. */
  87.  
  88. void main()
  89. {
  90.     long ticksStart;
  91.     /* We are NOT initializing any managers.  We're in the background, with no */
  92.     /* face, we can't use windows or dialogs or menus.  If you need to talk to the */
  93.     /* user you can post a notification, or launch an application to comunicate */
  94.     /* Passing an AppleEvent in the launchapplication trap could do the */
  95.     /* communication for you. */
  96.     
  97.     /* no nothing but events */
  98.     
  99.     InitAEStuff();
  100.     /* now initialize our PPC connection so people know we're around */
  101.     if(PPCInit() != noErr)ExitToShell(); /* bail */
  102.     ourPPCPtr = (MyPPCRecPtrDeamon)NewPtrClear(sizeof(MyPPCRecDeamon));
  103.     readBuffer = NewPtr(kOneK);
  104.     dataHandle = NewHandle(nil);                            /* get an empty handle to start */
  105.     /* check memory, bail if bad */
  106.     if(ourPPCPtr == nil || dataHandle == nil || readBuffer== nil)ExitToShell();
  107.     InformTheWorld();
  108.     ticksStart = TickCount();
  109.     /* no nothing but high level events */
  110.     while (gQuit == false) {
  111.         WaitNextEvent(highLevelEventMask, &ERecord, gMySleep, 0);
  112.         if (ERecord.what == kHighLevelEvent)
  113.             DoHighLevel(&ERecord);
  114.         if (gReadPending)
  115.             CollectLastData();
  116.         
  117.     }
  118.     CloseOffTheWorld();
  119. }
  120.  
  121. #undef __BUILDINGDEAMON__
  122. #undef __AEDMAIN__
  123.  
  124.  
  125.